﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using AutoStopData.Model;
using System.Linq;

namespace AutoStopData.BussinessLogic
{
    public static class Sessions
    {
        public static void Insert(Session entity)
        {
            using (var ctx = new AutoStopContext())
            {
                entity.SessionId = Guid.NewGuid();
                ctx.Sessions.AddObject(entity);
                ctx.SaveChanges();
            }
        }

        public static void UpdateLastAccess(Guid sessionId)
        {
            using (var ctx = new AutoStopContext())
            {
                var session = ctx.Sessions.First(o => o.SessionId == sessionId);
                session.LastAccess = DateTime.Now;
                ctx.SaveChanges();
            }
        }

        public static void UpdateSetLoggedOut(Guid sessionId)
        {
            using (var ctx = new AutoStopContext())
            {
                var session = ctx.Sessions.First(o => o.SessionId == sessionId);
                session.HasLoggedOut = true;
                ctx.SaveChanges();
            }
        }

        public static List<ActivitySummary> GetActivities(DateTime date)
        {
            var retVal = new List<ActivitySummary>();
            using (var ctx = new AutoStopContext())
            {
                var query =
                    from ses in ctx.Sessions
                    group ses by ses.UserId
                        into g
                        select new
                            {
                                sessions = g,
                                user = ctx.Users.FirstOrDefault(u => u.UserId == g.Key)
                            };

                Func<Session, bool> condition = s => s.LoggedIn.Date == date.Date;

                var queryResults = query.ToList().Where(o => o.sessions.Any(condition));
                foreach (var item in queryResults)
                {
                    var time = new TimeSpan();
                    var loggedIn = new DateTime();
                    var lastAccess = new DateTime();
                    var hasLoggedOut = true;
                    var found = false;
                    var subQuery = item.sessions.Where(condition);
                    foreach (var subItem in subQuery)
                    {
                        time = time.Add(subItem.LastAccess.Subtract(subItem.LoggedIn));
                        if (subItem.LoggedIn > loggedIn)
                        {
                            loggedIn = subItem.LoggedIn;
                            lastAccess = subItem.LastAccess;
                            hasLoggedOut = subItem.HasLoggedOut;
                            found = true;
                        }
                    }
                    if (found)
                    {
                        retVal.Add(new ActivitySummary
                                       {
                                           Mail = item.user.Mail,
                                           Time = time,
                                           LoggedIn = loggedIn,
                                           LastAccess = lastAccess,
                                           HasLoggedOut = hasLoggedOut
                            });
                    }
                }
            }
            return retVal;
        }
    }

    public class ActivitySummary
    {
        public string Mail { get; set; }
        public TimeSpan Time { get; set; }
        public DateTime LoggedIn { get; set; }
        public DateTime LastAccess { get; set; }
        public bool HasLoggedOut { get; set; }
        public double TimeMinutes
        {
            get
            {
                return Time.TotalMinutes;
            }
        }
    }
}
